package com.tanhua.server.service;


import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Video;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class VideoMQService {

    @Reference
    private VideoApi videoApi;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    /**
     * 发送消息公共方法
     * videoId 小视频id
     * type 类型 1-发布小视频，2-点赞，3-取消点赞，4-评论
     */
    public void sendMsg(String videoId, Integer type){
        // 根据小视频id查询小视频
        Video video = videoApi.findById(videoId);

        // 封装消息内容
        Map<String, Object> msg = new HashMap<>();
        msg.put("userId", UserHolder.getUserId());
        msg.put("vid", video.getVid());
        msg.put("type", type);

        rocketMQTemplate.convertAndSend("video", JSON.toJSONString(msg));

    }
}
